#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>

struct ListNode
{
    int val;
    struct ListNode* next;
};

struct ListNode* detectCycle(struct ListNode* head)
{
    struct ListNode* fast, * slow, * teg;
    fast = slow = head;

    while (fast && fast->next)
    {
        slow = slow->next;
        fast = fast->next->next;

        if (fast == slow)
        {
            struct ListNode* meet = fast;
            fast = head;

            while (fast != meet)
            {
                meet = meet->next;
                fast = fast->next;
            }

            return meet;
        }
    }

    return NULL;
}